package com.jingxing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.*;
import com.jingxing.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/playerUserInfo")
@Api(value = "API", tags = {"选手相关"})
@UserLoginToken
public class PlayerUserInfoController {
    @Autowired
    private PlayerUserInfoService playerUserInfoService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SignUpRewardService signUpRewardService;

    @Autowired
    private RewardReceiveService rewardReceiveService;

    @Autowired
    private MatchInfoService matchInfoService;

    @PostMapping("/savePlayerUserInfo")
    @ApiOperation("添加选手")
    @Transactional
    public ApiResult savePlayerUserInfo(PlayerUserInfo playerUserInfo) {
        if (StringUtils.isBlank(playerUserInfo.getName())) {
            return ApiResult.parameterError("姓名不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getPhotoUrl())) {
            return ApiResult.parameterError("照片地址不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getBirthday())) {
            return ApiResult.parameterError("生日不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getIdNumber())) {
            return ApiResult.parameterError("身份证号码不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getPhoneNumber()) || playerUserInfo.getPhoneNumber().length() != 11) {
            return ApiResult.parameterError("电话号码错误");
        }
        if (StringUtils.isBlank(playerUserInfo.getMailbox())) {
            return ApiResult.parameterError("邮箱不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getHomeAddress())) {
            return ApiResult.parameterError("家庭住址不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getWechatNumber())) {
            return ApiResult.parameterError("微信号不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getDouyinNumber())) {
            return ApiResult.parameterError("抖音号不能为空");
        }
        if (playerUserInfo.getDouyinFansNumber() == null) {
            return ApiResult.parameterError("抖音粉丝数不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getDeliveryExperience())) {
            return ApiResult.parameterError("带货经验不能为空");
        }
        if (StringUtils.isBlank(playerUserInfo.getGoodAtSkills())) {
            return ApiResult.parameterError("擅长技能不能为空");
        }

        // 粉丝数过1W标记新秀组
        if (playerUserInfo.getDouyinFansNumber() > 10000) {
            playerUserInfo.setPlayerLevel(1);
        }

        // 获取绑定最少客服id
        UserInfo minUserId = userInfoService.getMinUserId();
        if (minUserId == null) {
            return ApiResult.no(null, "无客服");
        }
        playerUserInfo.setSysUserId(minUserId.getId().toString());

        boolean save = playerUserInfoService.save(playerUserInfo);

        // 添加奖品领取
        if (save) {
            List<SignUpReward> list = signUpRewardService.list();
            if (list != null && list.size() > 0) {
                List<RewardReceive> receives = new ArrayList<>();
                for (SignUpReward signUpReward: list) {
                    RewardReceive rewardReceive = new RewardReceive();
                    rewardReceive.setPlayerId(playerUserInfo.getId());
                    rewardReceive.setPrizeId(signUpReward.getId());
                    rewardReceive.setReceiveSfrequency(signUpReward.getReceiveSfrequency());
                    receives.add(rewardReceive);
                }
                boolean b = rewardReceiveService.saveBatch(receives);
                return ApiResult.result(b);
            }
        }
        return ApiResult.result(save);
    }

    @PostMapping("/getPlayerUserInfos")
    @ApiOperation("获取选手信息")
    public ApiResult getPlayerUserInfos(String name, String phone) {
        List<PlayerUserInfo> playerUserInfos = playerUserInfoService.getPlayerUserInfos(name, phone);
        return ApiResult.ok(playerUserInfos);
    }

    @PostMapping("/getPlayerUserInfoById")
    @ApiOperation("通过id获取选手信息")
    public ApiResult getPlayerUserInfoById(String id) {
        PlayerUserInfo byId = playerUserInfoService.getById(id);
        if (byId != null) {
            UserInfo byId1 = userInfoService.getById(byId.getSysUserId());
            byId.setSysUserName(byId1.getName());
        }
        return ApiResult.ok(byId);
    }

    @PostMapping("/score")
    @ApiOperation("选手打分")
    public ApiResult score(String id, Integer rating, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (rating == null) {
            return ApiResult.parameterError("分数不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        PlayerUserInfo playerUserInfo = new PlayerUserInfo();
        playerUserInfo.setId(id);
        playerUserInfo.setRating(rating);
        playerUserInfo.setUpdateBy(updateBy);
        boolean update = playerUserInfoService.updateById(playerUserInfo);
        return ApiResult.result(update);
    }

    @PostMapping("/sign")
    @ApiOperation("选手签约")
    public ApiResult sign(String id, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        PlayerUserInfo playerUserInfo = new PlayerUserInfo();
        playerUserInfo.setId(id);
        playerUserInfo.setSignAContractStatus(1);
        playerUserInfo.setUpdateBy(updateBy);
        boolean update = playerUserInfoService.updateById(playerUserInfo);
        return ApiResult.result(update);
    }

    @PostMapping("/promotionPlayer")
    @ApiOperation("选手晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayer(String id, Integer promotionStatus, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (promotionStatus == null) {
            return ApiResult.parameterError("晋级状态不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        PlayerUserInfo playerUserInfo = new PlayerUserInfo();
        playerUserInfo.setId(id);
        playerUserInfo.setPromotionStatus(promotionStatus);
        playerUserInfo.setUpdateBy(updateBy);
        boolean update = playerUserInfoService.updateById(playerUserInfo);

        // 晋级添加
        if (update && promotionStatus == 1) {
            MatchInfo matchInfo = new MatchInfo();
            matchInfo.setPlayerId(id);
            matchInfo.setCompetitionLevel(0);
            matchInfo.setCreateBy(updateBy);
            boolean save = matchInfoService.save(matchInfo);
            return ApiResult.result(save);
        }
        return ApiResult.result(update);
    }

    @PostMapping("/promotionPlayers")
    @ApiOperation("选手批量晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayers(List<String> ids, Integer promotionStatus, String updateBy) {
        if (ids == null || ids.size() == 0) {
            return ApiResult.parameterError("IDS不能为空");
        }
        if (promotionStatus == null) {
            return ApiResult.parameterError("晋级状态不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        List<PlayerUserInfo> playerUserInfos = new ArrayList<>();
        List<MatchInfo> matchInfos = new ArrayList<>();
        for (String id: ids) {
            PlayerUserInfo playerUserInfo = new PlayerUserInfo();
            playerUserInfo.setId(id);
            playerUserInfo.setPromotionStatus(promotionStatus);
            playerUserInfo.setUpdateBy(updateBy);
            playerUserInfos.add(playerUserInfo);

            // 预添加比赛信息
            MatchInfo matchInfo = new MatchInfo();
            matchInfo.setPlayerId(id);
            matchInfo.setCompetitionLevel(0);
            matchInfo.setCreateBy(updateBy);
            matchInfos.add(matchInfo);
        }
        boolean b = playerUserInfoService.updateBatchById(playerUserInfos);

        // 晋级添加
        if (b && promotionStatus == 1) {
            boolean save = matchInfoService.saveBatch(matchInfos);
            return ApiResult.result(save);
        }
        return ApiResult.result(b);
    }

    @PostMapping("/promotionSuperPlayer")
    @ApiOperation("新秀晋级")
    @Transactional
    public ApiResult promotionSuperPlayer(String updateBy) {
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        // 新秀选手
        QueryWrapper<PlayerUserInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("player_level", 1);
        List<PlayerUserInfo> list = playerUserInfoService.list(queryWrapper);
        List<MatchInfo> matchInfos = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (PlayerUserInfo playerUserInfo: list) {
                playerUserInfo.setPromotionStatus(1);
                playerUserInfo.setUpdateBy(updateBy);

                // 预添加比赛信息
                MatchInfo matchInfo = new MatchInfo();
                matchInfo.setPlayerId(playerUserInfo.getId());
                matchInfo.setCompetitionLevel(0);
                matchInfo.setCreateBy(updateBy);
                matchInfos.add(matchInfo);
            }
        }
        // 新秀选手晋级
        boolean update = playerUserInfoService.updateBatchById(list);

        // 晋级添加
        if (update) {
            boolean save = matchInfoService.saveBatch(matchInfos);
            return ApiResult.result(save);
        }
        return ApiResult.result(update);
    }

    @PostMapping("/promotionRepechage")
    @ApiOperation("淘汰选手晋级复活赛")
    public ApiResult promotionRepechage(String id, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        MatchInfo matchInfo = new MatchInfo();
        matchInfo.setPlayerId(id);
        matchInfo.setCompetitionLevel(9);
        matchInfo.setCreateBy(updateBy);
        boolean save = matchInfoService.save(matchInfo);
        return ApiResult.result(save);
    }


}
